import os
import codecs

POS = os.path.join(os.getcwd(), 'corpus', 'pos')
NEG = os.path.join(os.getcwd(), 'corpus', 'neg')
FIX_POS = os.path.join(os.getcwd(), 'fix_pos')
FIX_NEG = os.path.join(os.getcwd(), 'fix_neg')


def fix_corpus(dir_s, dir_t):
    count = 0
    for item in os.listdir(dir_s):
        src_file = os.path.join(dir_s, item)
        dest_file = os.path.join(dir_t, item)

        # 检查是否为文件
        if os.path.isfile(src_file):
            with open(src_file, 'rb') as f:
                s = f.read()
                try:
                    # 优先尝试 gb2312
                    fix_s = s.decode('gb2312')
                except UnicodeDecodeError:
                    try:
                        # 如果 gb2312 失败，再尝试 utf-8
                        fix_s = s.decode('utf-8')
                    except UnicodeDecodeError:
                        # 最后使用 utf-8-sig 进行解码
                        try:
                            fix_s = s.decode('utf-8-sig')
                        except UnicodeDecodeError as e:
                            print(f"Error decoding file {item}: {e}")
                            count += 1
                            continue  # 跳过无法解码的文件

                # 将内容以 GBK 格式写入新文件
                with codecs.open(dest_file, 'w', encoding='gbk') as ff:
                    ff.write(fix_s)
    print(count)


if __name__ == "__main__":
    # 创建目标文件夹
    os.makedirs(FIX_POS, exist_ok=True)
    os.makedirs(FIX_NEG, exist_ok=True)

    # 修复正面和负面文本
    fix_corpus(POS, FIX_POS)
    fix_corpus(NEG, FIX_NEG)
